package com.minisoft.mybatis;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.po.LikeTable;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import org.junit.Test;

import java.util.Collections;

/**
 * https://github.com/baomidou/generator
 * @author JueSu
 * @description: TODO
 * @date 2022/6/21 14:29
 */
public class Generator {

    /**
     *         this.parent = "com.baomidou";
     *         this.moduleName = "";
     *         this.entity = "entity";
     *         this.service = "service";
     *         this.serviceImpl = "service.impl";
     *         this.mapper = "mapper";
     *         this.xml = "mapper.xml";
     *         this.controller = "controller";
     */

    @Test
    public void testPool() {
        String url = "jdbc:mysql://172.16.51.75:6033/gf_customer_user?useUnicode=true&characterEncoding=utf-8&useSSL=false";
        String username = "gf_customer",password = "Ag2iABXjx1R263z6";
        String outputDir = "C:\\Users\\31594\\IdeaProjects\\cnhuafas-customer-pool\\src\\main\\java";
        String xmlOutputDir = "C:\\Users\\31594\\IdeaProjects\\cnhuafas-customer-pool\\src\\main\\resources\\mappers";
        String parent = "com.cnhuafas.customer.pool";
        String table = "cp_online_";
//        outputDir = "D:/";
//        xmlOutputDir = outputDir;
        doGen(url,username,password,outputDir,xmlOutputDir,parent,table);
    }

    @Test
    public void testCommunity() {
        String url = "jdbc:mysql://172.16.51.75:6053/gfcommunity?useUnicode=true&characterEncoding=utf-8&useSSL=false";
        String username = "gfcm",password = "NDr75a5QNt5j91To";
        String outputDir = "C:\\Users\\31594\\IdeaProjects\\gf-community-service\\src\\main\\java";
        String xmlOutputDir = "C:\\Users\\31594\\IdeaProjects\\gf-community-service\\src\\main\\resources\\mapper";
        String parent = "com.cnhuafas.community";
        String table = "benefit";
//        outputDir = "D:/";
//        xmlOutputDir = outputDir;
        doGen(url,username,password,outputDir,xmlOutputDir,parent,table);
    }
    @Test
    public void testCenter() {
        String url = "jdbc:mysql://172.16.51.75:6033/gf_customer_user?useUnicode=true&characterEncoding=utf-8&useSSL=false";
        String username = "gf_customer",password = "Ag2iABXjx1R263z6";
        String outputDir = "C:\\Users\\31594\\IdeaProjects\\cnhuafas-customer-center\\customer-center-user-service\\src\\main\\java";
        String xmlOutputDir = "C:\\Users\\31594\\IdeaProjects\\cnhuafas-customer-center\\customer-center-user-service\\src\\main\\resources\\mappers";
        String parent = "com.cnhuafas.center.user";
        String table = "customer_report_extra";
//        outputDir = "D:/";
//        xmlOutputDir = outputDir;
        doGen(url,username,password,outputDir,xmlOutputDir,parent,table);
    }

    @Test
    public void testActivity() {
        String url = "jdbc:mysql://172.16.51.75:6033/huafa_domain_promotion?useUnicode=true&characterEncoding=utf-8&useSSL=false";
        String username = "promotion",password = "4SgJF172k5nYiL5q";
        String outputDir = "C:\\Users\\31594\\IdeaProjects\\cnhuafas-customer-pool\\src\\main\\java";
        String xmlOutputDir = "C:\\Users\\31594\\IdeaProjects\\cnhuafas-customer-pool\\src\\main\\resources\\mappers";
        String parent = "com.cnhuafas.customer.pool";
        String table = "promotion_activity_partake_log";
        outputDir = "D:/gen";
        xmlOutputDir = outputDir;
        doGen(url,username,password,outputDir,xmlOutputDir,parent,table);
    }
    @Test
    public void testYdac() {
        String url = "jdbc:mysql://172.16.44.7:3306/gfydacdb?useUnicode=true&characterEncoding=utf-8&useSSL=false";
        String username = "gfydacdb",password = "gfydacdb.test";
        String outputDir = "C:\\Users\\31594\\IdeaProjects\\cnhuafas-customer-pool\\src\\main\\java";
        String xmlOutputDir = "C:\\Users\\31594\\IdeaProjects\\cnhuafas-customer-pool\\src\\main\\resources\\mappers";
        String parent = "com.cnhuafas.ydac.service";
        String table = "exh_appoint_log";
        outputDir = "D:/";
        xmlOutputDir = outputDir;
        doGen(url,username,password,outputDir,xmlOutputDir,parent,table);
    }
    @Test
    public void testExh() {
        String url = "jdbc:mysql://172.16.44.7:3306/gfydacdb?useUnicode=true&characterEncoding=utf-8&useSSL=false";
        String username = "gfydacdb",password = "gfydacdb.test";
        String outputDir = "C:\\Users\\31594\\IdeaProjects\\gf-customer-exhibition\\src\\main\\java";
        String xmlOutputDir = "C:\\Users\\31594\\IdeaProjects\\gf-customer-exhibition\\src\\main\\resources\\mapper";
        String parent = "com.cnhuafas.foreign";
        String table = "exh_appoint_log";
//        outputDir = "D:/";
//        xmlOutputDir = outputDir;
        doGen(url,username,password,outputDir,xmlOutputDir,parent,table);
    }
    @Test
    public void testZyt() {
        String url = "jdbc:mysql://172.16.51.75:6083/huafa_ru_shi_dev?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
        String username = "gf_mp_dev",password = "8M9y2gUaXR8Yrr97";
        String outputDir = "C:\\Users\\31594\\IdeaProjects\\zyt-center\\rs-modules\\rs-mini-program\\src\\main\\java";
        String xmlOutputDir = "C:\\Users\\31594\\IdeaProjects\\zyt-center\\rs-modules\\rs-mini-program\\src\\main\\resources\\mapper";
        String parent = "com.yq.rs.modules.miniprogram";
        String table = "sales_center_association";
//        outputDir = "D:/";
//        xmlOutputDir = outputDir;
        doGen(url,username,password,outputDir,xmlOutputDir,parent,table);
    }
    public void doGen(String url,String username,String password,
                      String outputDir, String xmlOutputDir, String parent, String table) {
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("JueSu") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(outputDir); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(parent) // 设置父包名
                            .moduleName("") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, xmlOutputDir)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.entityBuilder().enableLombok().enableChainModel().disableSerialVersionUID();
                    builder.controllerBuilder().enableRestStyle();
                    builder.mapperBuilder().enableBaseResultMap();
                    //自定义serviceImpl，不以ServiceImpl结尾
                    builder.serviceBuilder().convertServiceImplFileName(entityName -> entityName + ConstVal.SERVICE);
                    builder.likeTable(new LikeTable(table)) // 设置需要生成的表名
                            .addTablePrefix("t_", "c_");// 设置过滤表前缀

                })
                .templateEngine(new VelocityTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
